import requests
from lxml import etree

from captcha_demo import Demo

# http/https特性 无状态。
# 使用session对象，该对象中包含cookie。
if __name__ == "__main__":
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    session = requests.session()
    responseText = session.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx",
                               headers=headers).text
    tree = etree.HTML(responseText)

    with open("./file/RandCode.jpg", "wb") as fp:
        fp.write(
            session.get("https://so.gushiwen.cn" + tree.xpath("//*[@id='imgCode']/@src")[0], headers=headers).content)

    randCode = Demo("./file/RandCode.jpg").test()
    __VIEWSTATE = tree.xpath("//*[@id='__VIEWSTATE']/@value")[0]
    __VIEWSTATEGENERATOR = tree.xpath("//*[@id='__VIEWSTATEGENERATOR']/@value")[0]
    print(randCode)
    print(__VIEWSTATE)
    print(__VIEWSTATEGENERATOR)
    data = {
        "__VIEWSTATE": __VIEWSTATE,
        "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
        "from": "http://so.gushiwen.cn/user/collect.aspx",
        "email": "*****92869@qq.com",
        "pwd": "*****",
        "code": randCode,
        "denglu": "登录"
    }
    response = session.post(
        "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx",
        data=data,
        headers=headers)
    pageResponseText = response.text
    print(response.status_code)
    with open("./file/gushiwenwang.html", "w", encoding="utf-8") as fp:
        fp.write(pageResponseText)
